﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.ETL.Domain.TaskRuntime
{
    class RTParameter
    {
        /// <summary>
        /// 获取默认值
        /// </summary>
        /// <param name="typeString"></param>
        /// <returns></returns>
        public static object GetDefaultValue(string typeString)
        {
            if (typeString == typeof(DateTime).FullName)
            {
                return DateTime.Now;
            }
            else if (typeString == typeof(Guid).FullName)
            {
                return Guid.Empty;
            }
            else if (typeString == typeof(int).FullName)
            {
                return 0;
            }
            else if (typeString == typeof(double).FullName)
            {
                return 0;
            }
            else if (typeString == typeof(float).FullName)
            {
                return 0;
            }
            return null;
        }

        public static bool TryParse(string typeName, string valueString, out object oValue)
        {
            oValue = null;
            if (typeName == typeof(DateTime).FullName)
            {
                DateTime retValue = DateTime.Now;
                if(DateTime.TryParse(valueString, out retValue))
                {
                    oValue = retValue;
                    return true;
                }
                return false;
            }
            else if (typeName == typeof(Guid).FullName)
            {
                Guid retValue = Guid.Empty;
                if(Guid.TryParse(valueString, out retValue))
                {
                    oValue = retValue;
                    return true;
                }
                return false;
            }
            else if (typeName == typeof(int).FullName)
            {
                int retValue = 0;
                if(int.TryParse(valueString, out retValue))
                {
                    oValue = retValue;
                    return true;
                }
                return false;
            }
            else if (typeName == typeof(double).FullName)
            {
                double retValue = 0;
                if(double.TryParse(valueString, out retValue))
                {
                    oValue = retValue;
                    return true;
                }
                return false;
            }
            else if (typeName == typeof(float).FullName)
            {
                float retValue = 0;
                if(float.TryParse(valueString, out retValue))
                {
                    oValue = retValue;
                    return true;
                }
                return false;
            }
            else if (typeName == typeof(FileInfo).FullName)
            {
                FileInfo retValue = null;
                retValue = new FileInfo(valueString);
                oValue = retValue;
                return true;
            }
            oValue = valueString;
            return true;
        }

        /// <summary>
        /// 将valueString的值转换为制定类型的值
        /// </summary>
        /// <param name="typeName"></param>
        /// <param name="valueString"></param>
        /// <returns></returns>
        public static object GetValue(string typeName, string valueString)
        {
            if (typeName == typeof(DateTime).FullName)
            {
                DateTime retValue = DateTime.Now;
                DateTime.TryParse(valueString, out retValue);
                return retValue;
            }
            else if (typeName == typeof(Guid).FullName)
            {
                Guid retValue = Guid.Empty;
                Guid.TryParse(valueString, out retValue);
                return retValue;
            }
            else if (typeName == typeof(int).FullName)
            {
                int retValue = 0;
                int.TryParse(valueString, out retValue);
                return retValue;
            }
            else if (typeName == typeof(double).FullName)
            {
                double retValue = 0;
                double.TryParse(valueString, out retValue);
                return retValue;
            }
            else if (typeName == typeof(float).FullName)
            {
                float retValue = 0;
                float.TryParse(valueString, out retValue);
                return retValue;
            }
            else if(typeName == typeof(FileInfo).FullName)
            {
                FileInfo retValue = null;
                retValue = new FileInfo(valueString);
                return retValue;
            }
            return valueString;
        }

        /// <summary>
        /// 验证值是否符合数据类型
        /// </summary>
        /// <param name="typeName"></param>
        /// <param name="valueString"></param>
        /// <returns></returns>
        public static bool Validate(string typeName, string valueString)
        {
            if (typeName == typeof(DateTime).FullName)
            {
                DateTime retValue = DateTime.Now;
                return DateTime.TryParse(valueString, out retValue);
            }
            else if (typeName == typeof(Guid).FullName)
            {
                Guid retValue = Guid.Empty;
                return Guid.TryParse(valueString, out retValue);
            }
            else if (typeName == typeof(int).FullName)
            {
                int retValue = 0;
                return int.TryParse(valueString, out retValue);
            }
            else if (typeName == typeof(double).FullName)
            {
                double retValue = 0;
                return double.TryParse(valueString, out retValue);
            }
            else if (typeName == typeof(float).FullName)
            {
                float retValue = 0;
                return float.TryParse(valueString, out retValue);
            }
            return true;
        }


        /// <summary>
        /// 验证值是否符合数据类型
        /// </summary>
        /// <param name="dbTypeName"></param>
        /// <param name="valueString"></param>
        /// <returns></returns>
        public static bool Validate4DB(string dbTypeName, int dbTypeLength, string valueString)
        {
            if (dbTypeName == "datetime")
            {
                DateTime retValue = DateTime.Now;
                return DateTime.TryParse(valueString, out retValue);
            }
            else if (dbTypeName == "uniqueidentifier")
            {
                Guid retValue = Guid.Empty;
                return Guid.TryParse(valueString, out retValue);
            }
            else if (dbTypeName == "int")
            {
                int retValue = 0;
                return int.TryParse(valueString, out retValue);
            }
            else if (dbTypeName == "decimal")
            {
                double retValue = 0;
                return double.TryParse(valueString, out retValue);
            }
            else if (dbTypeName == "decimal")
            {
                float retValue = 0;
                return float.TryParse(valueString, out retValue);
            }
            else
            {
                // string
                if(valueString.Length > dbTypeLength)
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 验证数据类型并修改不符合的数据为符合的数据格式（主要针对字符串的截取）
        /// </summary>
        /// <param name="nullable"></param>
        /// <param name="dbTypeName"></param>
        /// <param name="dbTypeLength"></param>
        /// <param name="valueString"></param>
        /// <param name="dbValue">验证通过之后得到的数据库可用的值</param>
        /// <param name="changedValueString">验证出错之后得到的字符串</param>
        /// <returns></returns>
        public static bool Validate4DBAndChange(bool nullable, string dbTypeName, int dbTypeLength, string valueString, out object dbValue, out string changedValueString)
        {
            if (nullable && string.IsNullOrEmpty(valueString))
            {
                dbValue = DBNull.Value;
                changedValueString = null;
                return true;
            }

            dbValue = null;
            changedValueString = null;
            if (dbTypeName == "datetime")
            {
                DateTime retValue = DateTime.Now;
                if(DateTime.TryParse(valueString, out retValue))
                {
                    dbValue = retValue;
                    return true;
                }
                else
                {
                    changedValueString = valueString;
                    return false;
                }
            }
            else if (dbTypeName == "uniqueidentifier")
            {
                Guid retValue = Guid.Empty;
                if(Guid.TryParse(valueString, out retValue))
                {
                    dbValue = retValue;
                    return true;
                }
                else
                {
                    changedValueString = valueString;
                    return false;
                }
            }
            else if (dbTypeName == "int")
            {
                int retValue = 0;
                if(int.TryParse(valueString, out retValue))
                {
                    dbValue = retValue;
                    return true;
                }
                else
                {
                    changedValueString = valueString;
                    return false;
                }
            }
            else if (dbTypeName == "bit")
            {
                bool retValue = false;
                if (bool.TryParse(valueString, out retValue))
                {
                    dbValue = retValue;
                    return true;
                }
                else
                {
                    changedValueString = valueString;
                    return false;
                }
            }
            else if (dbTypeName == "decimal")
            {
                double retValue = 0;
                if(double.TryParse(valueString, out retValue))
                {
                    dbValue = retValue;
                    return true;
                }
                else
                {
                    changedValueString = valueString;
                    return false;
                }
            }
            else
            {
                // string
                if (valueString.Length >= dbTypeLength)
                {
                    changedValueString = new string(valueString.ToCharArray(0, dbTypeLength - 1));
                    return false;
                }
                else
                {
                    dbValue = valueString;
                    changedValueString = valueString;
                }
            }
            return true;
        }

        public static string GetValueFromRTContext(RTParameterContext context, string expression)
        {
            string value = context.Run(expression);
            Logger.InfoToTag(Library.LogTAG, "参数转换将表达式:{0}转换为值:{1}", expression, value);
            return value;
        }
    }
}
